package com.wyl.mybatis.service;

import cn.hutool.json.JSONUtil;
import com.wyl.mybatis.config.SqlSessionFactoryConfig;
import com.wyl.mybatis.entity.FullCity;
import com.wyl.mybatis.mapper.FullCityMapper;
import com.wyl.mybatis.page.Page;
import com.wyl.mybatis.req.FullCityReq;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Description
 * @Author WuYiLong
 * @Date 2024/2/26 16:04
 */
public class MybatisService {

    private final static Logger log = LoggerFactory.getLogger(MybatisService.class);

    public static void main(String[] args) throws IOException {
        // 获取当前的数据库名称和版本号

        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryConfig.buildSqlSessionFactory();
        // SqlSessionFactory 通过解析配置文件（通常是 mybatis-config.xml）和映射文件（mapper.xml），来创建一个能够执行映射语句的会话对象SqlSession
        try(SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE)) {
            FullCityMapper mapper = sqlSession.getMapper(FullCityMapper.class);

            // 使用SqlSession获取映射器实例

            // 使用映射器执行操作
//            FullCity fullCity = mapper.selectByName("广东省");
//            FullCity fullCity1 = mapper.selectByNameAndCode("广东省", "44");
//            FullCityReq fullCityReq = new FullCityReq();
//            fullCityReq.setName("广东省");
//            FullCity fullCity2 = mapper.selectFullCity(fullCityReq);
//            List<FullCity> fullCities = mapper.selectFullCityLikeName("东");
//            System.out.println("城市的名称："+fullCity.getName());

            // 分页
            for (int i = 1; i <= 3 ; i++) {
                Page<FullCity> page = new Page<>(i, 10);
                List<FullCity> fullCities = mapper.selectFullCityPage(page);
                log.info("当前页：{}",page.getCurrentPage());
                log.info("页数：{}",page.getPageSize());
                log.info("总数：{}",page.getTotal());
                log.info("列表：{}", JSONUtil.toJsonStr(fullCities));
            }

//            Page<FullCity> page = new Page<>(3, 10);
//            List<FullCity> fullCities = mapper.selectFullCityPage(page);
//            log.info("当前页：{}",page.getCurrentPage());
//            log.info("页数：{}",page.getPageSize());
//            log.info("总数：{}",page.getTotal());
//            log.info("列表：{}", JSONUtil.toJsonStr(fullCities));

        }
    }


    private static void insert() {

    }

    private static void delete() {

    }

    private static void update() {

    }

    private static void select() {

    }


}
